<template>
  <div>
    <el-card>
      <div>
        <el-row type="flex" justify="space-between">
          <el-button type="primary" size="small" @click="addAgent">添加房产</el-button>
          <el-col :span="12">
            <el-input>
              <el-button slot="append" icon="el-icon-search" />
            </el-input>
          </el-col>
        </el-row>
      </div>
      <div class="table-content">
        <el-table :data="tableData" border>
          <el-table-column label="封面" prop="image">
            <template v-slot="{row}">
              <img style="width:180px" :src="row.image" alt="">
            </template>
          </el-table-column>
          <el-table-column label="标题" prop="name" />
          <el-table-column label="标签" prop="name" />
          <el-table-column label="地址" prop="name" />
          <el-table-column label="分类" prop="email" />
          <el-table-column label="经济" prop="phone" />
          <el-table-column label="价格" prop="address" />
          <el-table-column label="介绍" prop="desc" />
          <el-table-column label="图片" prop="image">
            <template v-slot="{row}">
              <img style="width:180px" :src="row.image" alt="">
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <el-button size="small" type="primary">编辑</el-button>
            <el-button size="small" type="danger">删除</el-button>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="center">
          <el-pagination
            layout="prev, pager, next"
            :total="50"
          />
        </el-row>
      </div>
    </el-card>
    <el-dialog title="新增经纪" :visible.sync="showDialog">
      <el-form ref="form" :rules="rules" :model="formData" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.name" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="formData.phone" placeholder="请输入电话号码" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="formData.desc" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="封面">
          <ImageUpload ref="imgupload" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-row type="flex" justify="center">
          <el-button size="small" type="primary" @click="confirmFn">确认</el-button>
          <el-button size="small">取消</el-button>
        </el-row>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getProperty } from '@/api/property'
export default {
  data() {
    return {
      showDialog: false,
      tableData: [],
      formData: {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: ''
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '电话不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '电话号码格式不正确', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '地址不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    const res = await getAgents({
      _limit: 2,
      _start: 0
    })
    this.tableData = res.data
    console.log(80, res.data)
  },
  methods: {
    // 新增经纪
    addAgent() {
      this.showDialog = true
    },
    // 确认 / 新增
    confirmFn() {
      this.$refs.form.validate()
    }
  }
}
</script>

<style>
.table-content{
  margin-top: 10px;
}
</style>
